<template>
  <div>
    <el-table :data="reviews" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="productName" label="商品名称"></el-table-column>
      <el-table-column prop="userName" label="用户"></el-table-column>
      <el-table-column label="评分" width="120">
        <template #default="{ row }">
          <el-rate v-model="row.rating" disabled></el-rate>
        </template>
      </el-table-column>
      <el-table-column label="评价内容">
        <template #default="{ row }">
          <div>{{ row.content }}</div>
          <div
            v-if="row.reply"
            style="
              margin-top: 5px;
              color: #666;
              background: #f5f5f5;
              padding: 5px;
            "
          >
            回复：{{ row.reply }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="评价时间"
        width="180"
      ></el-table-column>
      <el-table-column label="状态" width="120">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'danger'">
            {{ row.status === 1 ? "显示" : "隐藏" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button size="small" @click="showReplyDialog(row)">回复</el-button>
          <el-button
            size="small"
            :type="row.status === 1 ? 'warning' : 'success'"
            @click="toggleStatus(row)"
          >
            {{ row.status === 1 ? "隐藏" : "显示" }}
          </el-button>
          <el-button size="small" type="danger" @click="deleteReview(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="replyDialogVisible" title="回复评价" width="50%">
      <el-form :model="currentReview">
        <el-form-item label="评价内容">
          <el-input
            type="textarea"
            v-model="currentReview.content"
            :rows="3"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item label="回复内容">
          <el-input
            type="textarea"
            v-model="currentReview.reply"
            :rows="3"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="replyDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveReply">保存</el-button>
      </template>
    </el-dialog>

    <el-pagination
      v-model:current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next, total"
      @current-change="fetchReviews"
      style="margin-top: 20px"
    ></el-pagination>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import productsData from "./products.json";

const reviews = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const replyDialogVisible = ref(false);
const currentReview = ref({
  id: null,
  content: "",
  reply: "",
});

const fetchReviews = async () => {
  // Pagination logic
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;

  reviews.value = productsData.reviews.slice(start, end);
  total.value = productsData.reviews.length;
};

const showReplyDialog = (review) => {
  currentReview.value = { ...review };
  replyDialogVisible.value = true;
};

const saveReply = () => {
  const index = reviews.value.findIndex((r) => r.id === currentReview.value.id);
  if (index !== -1) {
    reviews.value[index].reply = currentReview.value.reply;
    ElMessage.success("回复成功");
  }
  replyDialogVisible.value = false;
};

const toggleStatus = (review) => {
  review.status = review.status === 1 ? 0 : 1;
  ElMessage.success(`评价已${review.status === 1 ? "显示" : "隐藏"}`);
};

const deleteReview = (review) => {
  ElMessageBox.confirm("确定删除此评价吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      reviews.value = reviews.value.filter((r) => r.id !== review.id);
      total.value -= 1;
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};

onMounted(() => {
  fetchReviews();
});
</script>
